package edu.bit.water.controller;

import org.springframework.beans.factory.annotation.Autowired;
//import org.iscas.web.service.UserService;
//import org.iscas.web.entity.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import edu.bit.water.dao.UserDao;
import edu.bit.water.entity.UserEntity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public abstract class BaseController {

	@Autowired
	protected HttpSession session;
	
	@Autowired
	protected UserDao userDao;
	//返回session中的context
	protected Context context() {
		Context ctx = (Context) session.getAttribute("context");
		if (ctx == null) {
			ctx = new Context();
			session.setAttribute("context", ctx);
		}
		return ctx;
	}

	protected UserEntity currentUser() {
		Integer userId = currentUserId();
		return null == userId ? null : userDao.findById(userId);

		//		return userDao.findById(13l).get();
	}

	protected Integer currentUserId() {
		Context ctx = context();
		return ctx == null ? null : ctx.userId;
	}
	
	protected void clearSession() {
		session.removeAttribute("context");
		session.removeAttribute("user");
	}
	
	protected HttpServletRequest getHttpServletRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder
			.getRequestAttributes())
			.getRequest();
		return request;
	}
	/*
	protected boolean isAdmin() {
		return currentUser().getRole().getId() == 1;
	}*/

}
